package com.ruoyi.material.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.material.domain.FoodAddRecord;
import com.ruoyi.material.mapper.FoodAddRecordMapper;
import com.ruoyi.material.service.IFoodAddRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 食品添加剂使用记录Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class FoodAddRecordServiceImpl implements IFoodAddRecordService {
    @Resource
    private FoodAddRecordMapper foodAddRecordMapper;

    /**
     * 查询食品添加剂使用记录
     *
     * @param id 食品添加剂使用记录主键
     * @return 食品添加剂使用记录
     */
    @Override
    public FoodAddRecord selectFoodAddRecordById(Long id) {
        FoodAddRecord foodAddRecord = foodAddRecordMapper.selectFoodAddRecordById(id);
        if (foodAddRecord.getUseBy() != null) {
            foodAddRecord.setUseByName(foodAddRecordMapper.selectUseByName(foodAddRecord.getUseBy()));
        }
        return foodAddRecord;
    }

    /**
     * 查询食品添加剂使用记录列表
     *
     * @param foodAddRecord 食品添加剂使用记录
     * @return 食品添加剂使用记录
     */
    @Override
    public List<FoodAddRecord> selectFoodAddRecordList(FoodAddRecord foodAddRecord) {
        if (foodAddRecord.getParams() != null) {
            if (foodAddRecord.getParams().containsKey("createTimeStart") && foodAddRecord.getParams().containsKey("createTimeEnd")) {
                if (foodAddRecord.getParams().get("createTimeStart") != null && !"".equals(foodAddRecord.getParams().get("createTimeStart"))
                        && foodAddRecord.getParams().get("createTimeEnd") != null && !"".equals(foodAddRecord.getParams().get("createTimeEnd"))) {
                    foodAddRecord.getParams().put("createTimeStart", foodAddRecord.getParams().get("createTimeStart") + " 00:00:00");
                    foodAddRecord.getParams().put("createTimeEnd", foodAddRecord.getParams().get("createTimeEnd") + " 23:59:59");
                }

            }
        }
        List<FoodAddRecord> foodAddRecords = foodAddRecordMapper.selectFoodAddRecordList(foodAddRecord);
        foodAddRecords.forEach(item -> {
            if (item.getUseBy() != null) {
                item.setUseByName(foodAddRecordMapper.selectUseByName(item.getUseBy()));
            }
        });
        return foodAddRecords;
    }

    /**
     * 新增食品添加剂使用记录
     *
     * @param foodAddRecord 食品添加剂使用记录
     * @return 结果
     */
    @Override
    public int insertFoodAddRecord(FoodAddRecord foodAddRecord) {
        foodAddRecord.setCreateTime(DateUtils.getNowDate());
        return foodAddRecordMapper.insertFoodAddRecord(foodAddRecord);
    }

    /**
     * 修改食品添加剂使用记录
     *
     * @param foodAddRecord 食品添加剂使用记录
     * @return 结果
     */
    @Override
    public int updateFoodAddRecord(FoodAddRecord foodAddRecord) {
        return foodAddRecordMapper.updateFoodAddRecord(foodAddRecord);
    }

    /**
     * 批量删除食品添加剂使用记录
     *
     * @param ids 需要删除的食品添加剂使用记录主键
     * @return 结果
     */
    @Override
    public int deleteFoodAddRecordByIds(Long[] ids) {
        return foodAddRecordMapper.deleteFoodAddRecordByIds(ids);
    }

    /**
     * 删除食品添加剂使用记录信息
     *
     * @param id 食品添加剂使用记录主键
     * @return 结果
     */
    @Override
    public int deleteFoodAddRecordById(Long id) {
        return foodAddRecordMapper.deleteFoodAddRecordById(id);
    }
}
